﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using System.Text;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin.order
{
    public partial class sendOrderList : BaseAdmin
    {

        DALCardType theCardType = new DALCardType();
        DALOrder theOrder = new DALOrder();
        dalCompany theCompany = new dalCompany();
        DALCardOrderFile theCardOrderFile = new DALCardOrderFile();
        tbCardOrderFile oCardOrderFile;




        protected void Grid1_Sort(object sender, ExtAspNet.GridSortEventArgs e)
        {
            DataTable table = theOrder.GetListByPage(GetSql(), string.Format("{0} {1}", e.SortField, e.SortDirection), Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            ViewState["sort"] = e.SortField;
            ViewState["dir"] = e.SortDirection;
        }




        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            GridViewBind();
        }

        private string GetSql()
        {
            string sql = string.Format(" adminId={0} ", base.AdminUserId);
            if (this.txtOrderCode.Text.Trim() != "")
            {
                sql = sql + " and ordercode like '%" + this.txtOrderCode.Text.Trim() + "%'";
            }
       

            if (this.txtAddress.Text.ToString() == "")
            {
                sql = sql + " and receiverAddress like '%" + this.txtAddress.Text.ToString().Trim() + "%' ";
            }

            if (this.txtCname.Text.ToString() != "")
            {
                sql = sql + " and receiver like '%" + this.txtCname.Text.ToString().Trim() + "%' ";
            }

            if (this.txtLinkWay.Text.ToString() != "")
            {
                sql = sql + " and receiverPhone like '%" + this.txtLinkWay.Text.ToString().Trim() + "%'";
            }
            if (this.dpSendDateFrom.Text.Trim() != "")
            {
                sql = sql + " and (inputTime >= '" + this.dpSendDateFrom.Text.Trim() + "' or inputTime is null)";
            }
            if (this.dpSendDateTo.Text.Trim() != "")
            {
                sql = sql + " and (inputTime <= '" + this.dpSendDateTo.Text.Trim() + " 23:59' or inputTime is null)";
            }


            return sql;
        }

        private void GridViewBind()
        {

            string sSql = GetSql();
            Grid1.RecordCount =  this.theCardOrderFile.GetRecordCount(sSql);
            string strOrderBy = string.Format("{0} {1} "
                                               , ViewState["sort"] == null ? null : ViewState["sort"].ToString()
                                               , ViewState["dir"] == null ? null : ViewState["dir"].ToString());
            if (strOrderBy.Length < 13)
            {
                strOrderBy = "inputTime desc ";
            }
            DataTable table = theCardOrderFile.GetListByPage(sSql, strOrderBy, Grid1.PageIndex, Grid1.PageSize).Tables[0];
            Grid1.DataSource = table;
            Grid1.DataBind();
            Grid1.SelectedRowIndexArray = new int[] { };
        }





        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.MySendOrder);

                this.GridViewBind();
            }
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.GridViewBind();
        }

        protected void btnInputOrder_Click(object sender, EventArgs e)
        {

            if (this.fuInput.HasFile && this.fuInput.FileName.Contains(".xls"))
            {
                string fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
                this.fuInput.SaveAs(Server.MapPath("../uploadTempFile/" + fileName));
                DataTable dtResult = new DataTable();
                dtResult = Gift.Utile.ExcelHelper.ImportDataTableFromExcel(Server.MapPath("../uploadTempFile/" + fileName), "文件列表", 0);
                DataRow[] dr = dtResult.Select();
                int ss = dtResult.Rows.Count;
                int rowsnum = dtResult.Rows.Count;
                if (rowsnum == 0)
                {
                    ExtAspNet.Alert.Show("Excel表为空表,无数据！");
                    return;
                }
                DateTime dtInputTime = DateTime.Now;

                for (int i = 0; i < rowsnum; i++)
                {
                    if (dr[i]["寄件客户编码_订单编号"].ToString().Trim() == "")
                    {
                        continue;
                    }
                    if(!theCardOrderFile.Exists(dr[i]["寄件客户编码_订单编号"].ToString().Trim(),dr[i]["商品名称"].ToString().Trim(),int.Parse(base.AdminUserId)))
                    {
                        oCardOrderFile = new tbCardOrderFile();
                        oCardOrderFile.orderCode = dr[i]["寄件客户编码_订单编号"].ToString().Trim();
                        oCardOrderFile.goodsName = dr[i]["商品名称"].ToString().Trim();
                        oCardOrderFile.goodsCount = int.Parse( dr[i]["数量"].ToString().Trim());
                        oCardOrderFile.receiver = dr[i]["收货人"].ToString().Trim();
                        oCardOrderFile.receiverPhone = dr[i]["收货人联系电话"].ToString().Trim();
                        oCardOrderFile.receiverAddress = dr[i]["收货地址"].ToString().Trim();
                        oCardOrderFile.remark = dr[i]["备注"].ToString().Trim();
                        oCardOrderFile.inputTime = dtInputTime;
                        oCardOrderFile.adminId = int.Parse(base.AdminUserId);
                        theCardOrderFile.Add(oCardOrderFile);
                   }

                }
                ExtAspNet.Alert.Show("成功导入" + rowsnum.ToString() + "条信息！");
                this.fuInput.Text = "";
                this.GridViewBind();
            }
            else
            {
                ExtAspNet.Alert.Show("请添加要导入的EXCEL文件！");
                return;
            }
        }




    }
}